x <- 1:5
y <- x * 2
z <- x + y
zSession 1 Homework
Try these homework questions to revise the content of session 1.
- What would be the result of evaluating the following in R?
2 + 3 * 4(2 + 3) * 42 == 3x <- 32read_csv("data.csv")mean(iris$Sepal.Length)tail(iris)!FALSEsum(c(1, 2, 5, 9))class(TRUE)mean(c("cat", "dog", "fish"))
Hint
You can try these in RStudio to check your answers!- I’ve written the following code in R. What would be the output? What are the values of
x,y, andzafter running all of this code?
- I’m trying to run the following code in R, but I got an error. Why? There might be more than one error.
2020_data <- read_csv("2020_data.csv")- Load the
mtcarsdata into your R environment using thedata()function.- What are the first 10 rows of the
mtcarsdataset? - What is the mean of the
mpgcolumn in themtcarsdataset?
- What are the first 10 rows of the
Hint
For (a),head() gives the first six rows of a dataset by default. Check the documentation for the head() function to see how to get the first 10 rows. For (b), you can use the mean() function to calculate the mean of a column in a dataset.
- Challenge: using the
irisdataset, I want to convert theSepal.Lengthcolumn to centimeters (it’s currently in inches) and add the result as a new column toiris. There are 2.54 centimeters in an inch. How would you do this in R?
Hint
You can create a new column in theiris dataset by assigning a vector to a new column name. For example, iris$new_column <- ...
If you have any questions, post them on the teams group. If you’d like more practice, try out the practice questions in the course ebook.
Just for fun: customising your RStudio!
RStudio is highly customisable, and you can change the appearance of the interface to suit your preferences. Try changing the theme, font size, or layout of RStudio to make it more comfortable for you to work in. You can find these options in the RStudio settings (Tools > Global Options):
For example, you can change the theme in the ‘Appearance’ section:
Here’s the ‘dracula’ theme:
Find a theme that you like and make RStudio your own!